-- 人物类
local Figure = class("Figure", function()
    return display.newSprite()
end)
Figure.TAG_ANIMATE = 0xfffff0

function Figure:ctor(nType, nFigureNum)
    self.m_nState = FigureState.Stand
    self.m_nDirection = FigureDirection.Down
    self.m_nFrameDirection = FigureFrameDirection.Down
    self.m_nFrameRate = 0.5
    self.m_nTexturePathType = nType
    self.m_nFigureNum = math.floor(nFigureNum)
    
    TextureController.addSpriteFrames(self.m_nTexturePathType, self.m_nFigureNum, handler(self, self.updateFigure))

    self:setContentSize(256 * 0.8, 256 * 0.8)
end

-- 设置状态和方向
function Figure:setFigureState(nState, nDirection)
    local bIsChange = false
    
    if (nState and nState ~= FigureState.None and nState ~= self.m_nState) then
        self.m_nState = nState
        bIsChange = true
    end
    
    if (nDirection and nDirection ~= FigureDirection.None and nDirection ~= self.m_nDirection) then
        self.m_nDirection = nDirection
        bIsChange = true
    end

    if (bIsChange) then
        self:updateFigure()
    end
end

-- 设置朝向
function Figure:setFrameDirection(nDirection)
    if (nDirection and nDirection ~= FigureFrameDirection.None and nDirection ~= self.m_nFrameDirection) then
        self.m_nFrameDirection = nDirection
    end
end

local function getFrameRate(nState, nType)
    local nFrameRate = 0
    
    if (nType == TexturePathType.Figure) then
        if (nState == FigureState.Stand) then
            nFrameRate = 1/3.0
        elseif (nState == FigureState.Walk) then
            nFrameRate = 0.6/16.0
        elseif (nState == FigureState.Run) then
            nFrameRate = 0.6/16.0
        elseif (nState == FigureState.Attack) then
            nFrameRate = 1/8.0     
        elseif (nState == FigureState.Caster) then
            nFrameRate = 1/8.0
        elseif (nState == FigureState.Hurt) then
            nFrameRate = 1/8.0
        elseif (nState == FigureState.Death) then
            nFrameRate = 1/4.0
        end
    end
    
    return nFrameRate
end

-- 更新人物状态
function Figure:updateFigure()
	self.m_nFrameRate = getFrameRate(self.m_nState, self.m_nTexturePathType)
    if (self.m_nDirection == FigureDirection.Left or 
        self.m_nDirection == FigureDirection.LeftAndUp or 
        self.m_nDirection == FigureDirection.LeftAndDown) then
        self:setRotationSkewY(180)
    else
        self:setRotationSkewY(0)
    end
    
    self:runActions()
end

function Figure:runActions()
    print("runActions")
    self:stopActionByTag(Figure.TAG_ANIMATE)
    
    local figurePath = TextureController.getTexturePath(TexturePathType.Figure, self.m_nFigureNum)
    local animate = self:getRunActionsFromSprite(figurePath)
    

    local repeatForever = cc.RepeatForever:create(animate)
    repeatForever:setTag(Figure.TAG_ANIMATE)
    self:runAction(repeatForever)
end

function Figure:getRunActionsFromSprite(path)
    local nDirection = self.m_nFrameDirection
    if nDirection == 3 then
        nDirection = 1
    end

    local flag = 0
    local array = {}
    
    while (true) do
        local frameName = string.format("%s_%d_%d_%02d.png", path, self.m_nState, nDirection*2, flag)
        local frame = cc.SpriteFrameCache:getInstance():getSpriteFrame(frameName)
        if (not frame) then
            break
        end
        table.insert(array, frame)
        flag = flag + 1
    end
    
    local animation = cc.Animation:createWithSpriteFrames(array, self.m_nFrameRate)
    local animate = cc.Animate:create(animation)
    return animate
end

-- 脚
function Figure:getAnchorPointWithFoot()
    local x = self:getContentSize().width / 2 / self:getContentSize().width
    local y = (self:getContentSize().height - 120) / self:getContentSize().height
    return cc.p(x, y)
end

function Figure:getAnchorPointCenter()
    return cc.p(0.5, 0.5)
end

-- 设置透明度
function Figure:setOpacityEx(opacity)
    self:setOpacity(opacity)
end

return Figure